import { wxRequest,session,moduleGenerator} from '@/utils/request.js';
import {
	removeStorage,
	setStorage,
	getStorage,
	showToast
} from '@/utils/util.js'
const _state = {
	user_info: {}
};
if(session.access_token) {
	_state.user_info = session.user_info;
}
const updateUserCache = function(commit) {
	return new Promise((resolve, reject)=>{
		wxRequest('uni/UserInfo').then(res => {
			session.user_info = res;
			uni.setStorage({ key: 'session', data: session });
			commit('SET_USER_INFO', res);
			resolve(res);
		}).catch(error=>reject)
	});
};

const _actions = {
	//获取信息
	getData({ commit }, params={}) {
		const {urlType,...param} = params;
		return new Promise((resolve, reject)=>{
			wxRequest(`user/${urlType}`, param,'POST').then(res => {
				resolve(res);
			}).catch(error=>reject)
		})
	},
	osUser({ commit }, params={}) {
		const {urlType,...param} = params;
		return new Promise((resolve, reject)=>{
			wxRequest(`uni/${urlType}`, param,'POST').then(res => {
				resolve(res);
			}).catch(error=>reject)
		})
	},
	//forceRequest表示不读缓存，强制发起请求
	// get_user_info({ commit }, params={}) {
	// 	if(!params.forceRequest && session.access_token) {
	// 		return new Promise((resolve, reject)=>{
	// 			commit('SET_USER_INFO', session.user_info);
	// 			resolve(session.user_info);
	// 		})
	// 	}
	// 	return new Promise((resolve, reject)=>{
	// 		updateUserCache(commit).then(res=>{
	// 			commit('SET_USER_INFO', res);
	// 			resolve(res);
	// 		}).catch(error=>reject);
	// 	})
	// }
};

const _mutations = {
	SET_STUDYLIST: (state, value) => {
		state.study_list = value;
	},
	SET_ROLE:(state,value) =>{
		state.role = value
	},
	SET_USER_INFO: (state,value) =>{
		state.user_info = value
	}
};

const {state,mutations,actions} = moduleGenerator('uni', [
		// {action:'ModifyUser',isPost:true},
		// {action:'UserInfo',isPost:true},
		// {action:'RegisterUser',isPost:true}
	])

export default {
	namespaced: true,
	state:{...state,..._state},
	mutations:{...mutations,..._mutations},
	actions:{...actions,..._actions}
};
